<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 发布ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

require 'app_class/issueInfoHandler.class.php';

// require_once('projman/release.php');
class ajax_issueInfo {
	
	public function execute(){
	
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
	
		switch ($operFlg){
			case 1:
				$this->showList();//显示列表
				break;
			case 2:
				$this->changeStatus();//变更发布状态
				break;
			case 3:
				$this->checkUser();//检测用户密码
				break;
			case 4:
				$this->issueOneVersionNumber();//显示版本号对应的文件
				break;
			case 5:
				$this->doIssue();//代码提交到测试服
				break;
			case 6:
				$this->do_compare();//文件比较
				break;
		}
	
	}
	
	/* 
	 * 显示列表
	 *  */
	private function showList() {
		
		global $G_PGLOB;

		//实例化issueInfoHandler
		$issueInfoHandler = new issueInfoHandler($G_PGLOB->DBCA);
		
		//将查询条件传入issueInfoHandler类的私有属性
		$issueInfoHandler->page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;                                //当前页
		
		$issueInfoHandler->rp = isset($_REQUEST['rp']) ? $_REQUEST['rp'] : 10;                                     //每页数
		
		$issueInfoHandler->sortname = isset($_REQUEST['sortname']) ? $_REQUEST['sortname'] : '';                   //排序的字段
		
		$issueInfoHandler->sortorder = isset($_REQUEST['sortorder']) ? $_REQUEST['sortorder'] : '';                //排序方式
		
		$username = $G_PGLOB -> sessionCache_get('username');
		
		$issueInfoHandler->issueUsername = $username;    //用户名
		
		if (isset($_REQUEST['issueType']) && !empty($_REQUEST['issueType'])){                              //发布状态
			
			switch ($_REQUEST['issueType']){
				case 1:
					$issueInfoHandler->issueFlg = 0;
					break;
				case 2:
					$issueInfoHandler->issueFlg = 1;
					break;
				case 3:
					$issueInfoHandler->issueServerFlg = 0;
					break;
				case 4:
					$issueInfoHandler->issueServerFlg = 1;
					break;
				case 5:
					$issueInfoHandler->issueServerFlg = 2;
					break;
			}
			
		}
		
		//查询方法
		$search_array = $issueInfoHandler->showIssueInfo();

		//页面输出数组
		$result_array = array(
				'page' => $_REQUEST['page'],
				'total' => $issueInfoHandler->total,
				'rows' => array()
		);
		
		//将查询结果组装进页面输出数组中
		if (!empty($search_array)){
			foreach ($search_array as $value){
				
				$testConfirm = '已确认';
				
				$testRollBack = '无法回滚';
				
				$formalConfirm = '已回滚';
				
				if ($value->issueFlg == 0){
					$testConfirm = '<a href="javascript:void(0);" onclick="check('.$value->issueID.','.$value->versionNumber.',1);">未确认</a>';
					$testRollBack = '<a href="javascript:void(0);" onclick="check('.$value->issueID.','.$value->versionNumber.',2);">回滚</a>';
				}elseif ($value->issueFlg == 2){
					$testRollBack = '已回滚';
				}
				
				if ($value->issueServerFlg == 0){
					$formalConfirm = '<a href="javascript:void(0);" onclick="check('.$value->issueID.','.$value->versionNumber.',3);">发布</a>';
				}elseif ($value->issueServerFlg == 1){
					$formalConfirm = '<a href="javascript:void(0);" onclick="check('.$value->issueID.','.$value->versionNumber.',4);">回滚</a>';
				}
				
				$result_array['rows'][] = array(
						'id' => $value->issueID,
						'cell' => array(
								$value->issueID,
								$value->issueUsername,
								$value->versionNumber,
								$value->issueTime,
								$testConfirm,
								$testRollBack,
								'<a href="javascript:void(0);" onclick="showDetail('.$value->issueID.','.$value->versionNumber.');">查看</a>',
								$formalConfirm
						)
				);
			}
		}
		
		echo json_encode($result_array);
		
	}
	
	/* 
	 * 将SVN的代码提交到服务器
	 *  */
	private function doIssue(){
		global $G_PGLOB;
		
		$result = array();
		
		if (!isset($_REQUEST['versionNumber']) || !is_numeric($_REQUEST['versionNumber'])){
			
			$result['code'] = 400;
			$result['message'] = '请选择要发布的版本号';
			
		}else{
			
			$versionNumber = trim($_REQUEST['versionNumber']);
			
			$dataBase = isset($_REQUEST['formValue']['dataBase']) ? $_REQUEST['formValue']['dataBase'] : '';
			
			$importData = array();
			
			$importData[CODE_RELEASE_PRODUCT_NAME] = CODE_RELEASE_TEST_SERVER;
			
			$issueInfoHandler = new issueInfoHandler($G_PGLOB->DBCA);
			
			$username = $G_PGLOB -> sessionCache_get('username');
			
			$issueInfoHandler->issueUsername = $username;
			
			$issueInfoHandler->versionNumber = $versionNumber;
			
			$issueInfoHandler->creator = $G_PGLOB->currentUserID;
			
			$result = $issueInfoHandler->deploy_do($importData);
			 
		}
		
		echo json_encode($result);
	}
	
	/* 
	 * 对比文件
	 *  */
	private function do_compare(){
		global $G_PGLOB;
		
		$result = array();
		
		if (!isset($_REQUEST['versionNumber']) || !is_numeric($_REQUEST['versionNumber'])){
			
			$result['code'] = 400;
			$result['message'] = '版本号传递错误';
			
		}elseif (!isset($_REQUEST['fileName']) || empty($_REQUEST['fileName'])){
			
			$result['code'] = 400;
			$result['message'] = '文件名传递错误';
			
		}else {
			
			$versionNumber = trim($_REQUEST['versionNumber']);
			$fileName = trim($_REQUEST['fileName']);
			
			$issueInfoHandler = new issueInfoHandler($G_PGLOB->DBCA);
			
			$issueInfoHandler->versionNumber = $versionNumber;
			
			$result = $issueInfoHandler->doCompare($fileName);
			
		}
		
		echo json_encode($result);
		
	}
	
	/* 
	 * 显示版本号对应的文件
	 *  */
	private function issueOneVersionNumber(){
		
		global $G_PGLOB;
		
		$result = array();
		
		if (!isset($_REQUEST['versionNumber']) || !is_numeric($_REQUEST['versionNumber'])){
			$result['code'] = 400;
			$result['message'] = '请选择要发布的版本号';
		}else {
			$versionNumber = trim($_REQUEST['versionNumber']);
			$issueInfoHandler = new issueInfoHandler($G_PGLOB->DBCA);
			$issueInfoHandler->versionNumber = $versionNumber;
			$result = $issueInfoHandler->doIssue();
		}
		
		echo json_encode($result);
		
	}
	
	/* 
	 * 改变发布状态
	 *  */
	private function changeStatus(){
		
		global $G_PGLOB;
		
		//发布号
		if (isset($_REQUEST['issueID']) && is_numeric($_REQUEST['issueID'])){
			$issueID = trim($_REQUEST['issueID']);
		}else {
			$issueID = null;
		}
		//版本号
		if (isset($_REQUEST['versionNumber']) && is_numeric($_REQUEST['versionNumber'])){
			$versionNumber = trim($_REQUEST['versionNumber']);
		}else {
			$versionNumber = null;
		}
		//flg
		if (isset($_REQUEST['flg']) && is_numeric($_REQUEST['flg'])){
			$flg = trim($_REQUEST['flg']);
		}else {
			$flg = null;
		}
		
		$issueInfoHandler = new issueInfoHandler($G_PGLOB->DBCA);
		
		$issueInfoHandler->issueID = $issueID;
		
		$issueInfoHandler->versionNumber = $versionNumber;
		
		$issueInfoHandler->flg = $flg;
		
		$result = $issueInfoHandler->doChange();
		
		echo json_encode($result);
		
	}
	
	/* 
	 * 检测用户密码
	 *  */
	private function checkUser(){
		
		global $G_PGLOB;
		
		$username = isset($_REQUEST['username']) ? trim($_REQUEST['username']) : null;
		$pwd = isset($_REQUEST['pwd']) ? trim($_REQUEST['pwd']) : null;
		
		$error_str = '';
		
		$user = new entity('s_userlogin');
		$user -> username = $username;
		$user -> pwd = md5($pwd);
		
		if (!$G_PGLOB -> DBCA -> fetch($user)){
			
			$error_str .= '密码错误';
			
			$error_msg = array('code' => 400, 'message' => $error_str);
			echo json_encode($error_msg);
			
		}else {
			
			$error_msg = array('code' => 200, 'message' => $error_str);
			echo json_encode($error_msg);
			
		}
		
	}
}